
import threading
import time
from time import sleep

class RetryException(Exception):
    """多次重试未能如愿，抛出错误重启App"""
    def __init__(self, info=''):
        self.info = info
    def __str__(self):
        return self.info

class TaskException(Exception):
    """浏览内容循环时主动抛出，以去做任务"""
    def __init__(self, info=''):
        self.info = info
    def __str__(self):
        return self.info
    
class SwitchException(Exception):
    """抛出错误，切换app"""
    def __init__(self, info=''):
        self.info = info
    def __str__(self):
        return self.info

class TimedException(Exception):
    """抛出错误，执行时间任务"""
    def __init__(self, info=''):
        self.info = info
    def __str__(self):
        return self.info

class DoneException(Exception):
    """滑动错误，此错误为浏览内容时，却未能正确进入浏览内容界面"""
    def __init__(self, info=''):
        self.info = info
    def __str__(self):
        return self.info
    
class PeriodException(Exception):
    """抛出错误，执行时间任务"""
    def __init__(self, info=''):
        self.info = info
    def __str__(self):
        return self.info
    
  
class FindException(Exception):
    """滑动错误，此错误为浏览内容时，却未能正确进入浏览内容界面"""
    def __init__(self, info=''):
        self.info = info
    def __str__(self):
        return self.info
    
class DayException(Exception):
    """一天结束后，报错以重新开始"""
    def __init__(self, info=''):
        self.info = info
    def __str__(self):
        return self.info
    
class NightException(Exception):
    """一天结束后，报错以重新开始"""
    def __init__(self, info=''):
        self.info = info
    def __str__(self):
        return self.info
    
class HourException(Exception):
    """小时任务"""
    def __init__(self, info=''):
        self.info = info
    def __str__(self):
        return self.info
class StopException(Exception):
    """小时任务"""
    def __init__(self, info=''):
        self.info = info
    def __str__(self):
        return self.info
    
    

def switch_task(task_control, app_name, after_work=None):
    """在循环中使用，以切换程序"""
    if task_control.get(app_name) == '跨夜':
        raise NightException
    elif task_control.get(app_name) == '定时':
        raise TimedException
    elif task_control.get(app_name) == '下班':
        if after_work:
            after_work()
        raise DoneException
    elif task_control.get(app_name) == '周期':
        raise PeriodException
    elif task_control.get(app_name) == '天':
        raise DayException
    elif task_control.get(app_name) == '小时':
        raise HourException()
    elif task_control.get(app_name) == '停止':
        raise StopException
   


class MyTimer(threading.Thread):
    def __init__(self, seconds, mytarget, myargs=None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.__flag = threading.Event()     # 用于暂停线程的标识
        self.__flag.set()       # 设置为True
        self.__running = threading.Event()      # 用C于停止线程的标识
        self.__running.set()      # 将running设置为True
        self.seconds = seconds
        self.mytarget = mytarget
        self.myargs = myargs

    def run(self):
        while self.__running.isSet():
            self.__flag.wait()      # 为True时立即返回, 为False时阻塞直到内部的标识位为True后返回
            sleep(self.seconds)
            if self.myargs:
                self.mytarget(*self.myargs)
            else:
                self.mytarget()

    def pause(self):
        self.__flag.clear()     # 设置为False, 让线程阻塞

    def resume(self):
        self.__flag.set()    # 设置为True, 让线程停止阻塞

    def stop(self):
        self.__flag.set()       # 将线程从暂停状态恢复, 如何已经暂停的话
        self.__running.clear()        # 设置为False    
